//
// Created by Administrator on 2022/2/4.
//

#ifndef CPP_LEARNING_LEETCODE341_H
#define CPP_LEARNING_LEETCODE341_H
#include <vector>
#include <deque>
using namespace std;
class NestedInteger {
       public:
         // Return true if this NestedInteger holds a single integer, rather than a nested list.
         bool isInteger() const;

         // Return the single integer that this NestedInteger holds, if it holds a single integer
         // The result is undefined if this NestedInteger holds a nested list
         int getInteger() const;

         // Return the nested list that this NestedInteger holds, if it holds a nested list
         // The result is undefined if this NestedInteger holds a single integer
         const vector<NestedInteger> &getList() const;
 };

//每个元素要么是一个整数，要么是一个列表；该列表的元素也可能是整数或者是其他列表
class NestedIterator {
private:
    deque<NestedInteger> m_nestedList;

public:
    NestedIterator(vector<NestedInteger> &nestedList) {
        for(int i=0; i<nestedList.size(); i++){
            this->m_nestedList.push_back(nestedList[i]);
        }
    }

    int next() {
        while (true){
            NestedInteger nestedInteger = m_nestedList.front();
            m_nestedList.pop_front();

            if(nestedInteger.isInteger()){
                return nestedInteger.getInteger();
            }else{
                vector<NestedInteger> nestedList = nestedInteger.getList();
                for(int i=nestedList.size()-1; i>=0; i--){
                    this->m_nestedList.push_front(nestedList[i]);
                }
            }
        }
        return 0;
    }
    bool isNotEmpty(const vector<NestedInteger>& nestedList){
        for(int i=0;i<nestedList.size(); i++){
            if(nestedList[i].isInteger()){
                return true;
            }
            else if(isNotEmpty(nestedList[i].getList())){
                return true;
            }
        }

        return false;
    }
    bool hasNext() {
        for(int i=0;i<m_nestedList.size(); i++){
            if(m_nestedList[i].isInteger()){
                return true;
            }else if(isNotEmpty(m_nestedList[i].getList())){
                return true;
            }
        }
        return false;

    }
};

#endif //CPP_LEARNING_LEETCODE341_H
